package com.study.seckill.web.api;

import com.study.dto.CourseSeckillDTO;
import com.study.seckill.entity.CourseSeckill;
import com.study.seckill.service.CourseSeckillService;
import com.study.seckill.vo.CourseSeckillDetailVO;
import com.study.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: CourseSeckillController
 * @Author: wYin
 * @Date: 2022/3/10 14:45
 */
@RestController("apiCourseSeckill")
@RequestMapping("/api/courseSeckill")
@Api(tags = "课程秒杀接口管理")
public class CourseSeckillController {
    @Autowired
    private CourseSeckillService courseSeckillService;

    @GetMapping("/showList")
    @ApiOperation(value="查询所有可秒杀的课程",notes = "秒杀课程展示")
    public ResponseResult<List<CourseSeckillDTO>> showList(){
        List<CourseSeckill> courseSeckills = courseSeckillService.showCourseSeckill();
        List<CourseSeckillDTO> courseSeckillDTOS = new ArrayList<>();
        courseSeckills.forEach(t->{
            CourseSeckillDTO courseSeckillDTO = new CourseSeckillDTO();
            BeanUtils.copyProperties(t,courseSeckillDTO);
            courseSeckillDTOS.add(courseSeckillDTO);
        });
        return new ResponseResult<>(courseSeckillDTOS);
    }

    @GetMapping("/item/{id}")
    public ResponseResult<CourseSeckillDetailVO> item(@PathVariable("id")Integer id){
        //根据秒杀id查询
        CourseSeckillDetailVO result = courseSeckillService.getSeckillInfoById(id);
        return new ResponseResult<>(result);
    }

//    @GetMapping("/getByName/{name}")
//    public List<CourseSeckill> getByName(@PathVariable("name") String name){
//        return courseSeckillService.getByName(name);
//    }
}
